package cn.com.enersun.iec61850.integratedDataProcessing;

import org.openmuc.openiec61850.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ServerSocketFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by YYC on 2017/4/1.
 */
public class ServerDataProcessing  implements ServerEventListener {
    private static final Logger logger = LoggerFactory.getLogger(ServerDataProcessing.class);

    private static ServerSap serverSap;
    private ServerNetParameter serverNetParameter;
    private ServerModel serverModel;
    private String name;
    private ServerSocketFactory serverSocketFactory;
    private ServerEventListener serverEventListener;

    public ServerDataProcessing() {
    }


    public ServerDataProcessing(ServerNetParameter serverNetParameter, ServerModel serverModel, String name, ServerSocketFactory serverSocketFactory) {
        this.serverNetParameter = serverNetParameter;
        this.serverModel = serverModel;
        this.name = name;
        this.serverSocketFactory = serverSocketFactory;
    }

    public static ServerSap getServerSap() {
        return serverSap;
    }

    public static void setServerSap(ServerSap serverSap) {
        ServerDataProcessing.serverSap = serverSap;
    }

    public ServerNetParameter getServerNetParameter() {
        return serverNetParameter;
    }

    public void setServerNetParameter(ServerNetParameter serverNetParameter) {
        this.serverNetParameter = serverNetParameter;
    }

    public ServerModel getServerModel() {
        return serverModel;
    }

    public void setServerModel(ServerModel serverModel) {
        this.serverModel = serverModel;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ServerSocketFactory getServerSocketFactory() {
        return serverSocketFactory;
    }

    public void setServerSocketFactory(ServerSocketFactory serverSocketFactory) {
        this.serverSocketFactory = serverSocketFactory;
    }

    public void startServer() throws IOException {

        serverSap = new ServerSap(
                serverNetParameter.getPort(),
                serverNetParameter.getBacklog(),
                serverNetParameter.bindAddr,
                serverModel,
                name,
                serverSocketFactory);

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                if (serverSap != null) {
                    serverSap.stop();
                }
                logger.error("Server was stopped.");
            }
        });

        // create a ServerDataProcessing instance that can be passed as a callback object to startListening() and
        // setDefaultWriteListener()
        serverSap.startListening(serverEventListener);
    }

    @Override
    public void serverStoppedListening(ServerSap serverSap) {
        logger.error("The SAP stopped listening");
    }

    @Override
    public List<ServiceError> write(List<BasicDataAttribute> bdas) {
        for (BasicDataAttribute bda : bdas) {
            logger.info("got a write request: " + bda);
        }
        return null;

    }
}
